package org.sith.gl.taskmanager.server.util;

import org.sith.gl.taskmanager.client.beans.TaskEntityGWT;
import org.sith.gl.taskmanager.client.beans.UserEntityGWT;
import org.sith.gl.taskmanager.server.db.dao.TaskEntity;
import org.sith.gl.taskmanager.server.db.dao.UserEntity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Utils {
    private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("mm/DD/yyyy");


    public static UserEntityGWT convertToUserGWTEntity(UserEntity userEntity) {
        UserEntityGWT userEntityGWT = new UserEntityGWT();
        userEntityGWT.setId(userEntity.getId());
        userEntityGWT.setLogin(userEntity.getLogin());
        return userEntityGWT;
    }


    public static List<TaskEntityGWT> convertToTaskGWTEntities(List<TaskEntity> taskEntities) {
        List<TaskEntityGWT> result = new ArrayList<TaskEntityGWT>();
        for (TaskEntity taskEntity : taskEntities) {
            TaskEntityGWT taskEntityGWT = convertToTaskGWTEntity(taskEntity);
            result.add(taskEntityGWT);
        }
        return result;
    }

    public static TaskEntityGWT convertToTaskGWTEntity(TaskEntity taskEntity) {
        TaskEntityGWT taskEntityGWT = new TaskEntityGWT();
        taskEntityGWT.setId(taskEntity.getId());
        taskEntityGWT.setCreatedDate(convertDateToString(taskEntity.getCreatedDate()));
        taskEntityGWT.setDescription(taskEntity.getDescription());

        taskEntityGWT.setAddedBy(convertToUserGWTEntity(taskEntity.getAddedBy()));
        taskEntityGWT.setAssignedTo(convertToUserGWTEntity(taskEntity.getAssignedTo()));

        taskEntityGWT.setDueDate(convertDateToString(taskEntity.getDueDate()));
        taskEntityGWT.setComplete(taskEntity.isComplete());
        taskEntityGWT.setCloseDescription(taskEntity.getCloseDescription());
        return taskEntityGWT;
    }


    private static String convertDateToString(Date date) {
        return FORMATTER.format(date);

    }


    public static List<TaskEntity> convertToTaskDBEntities(List<TaskEntityGWT> taskEntities) {
        List<TaskEntity> result = new ArrayList<TaskEntity>();
        for (TaskEntityGWT taskEntity : taskEntities) {
            TaskEntity taskEntityEx = convertToTaskDBEntity(taskEntity);
            result.add(taskEntityEx);
        }
        return result;


    }


    public static TaskEntity convertToTaskDBEntity(TaskEntityGWT taskEntityGWT) {
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setId(taskEntityGWT.getId());
        taskEntity.setCreatedDate(convertStringToDate(taskEntityGWT.getCreatedDate()));
        taskEntity.setDescription(taskEntityGWT.getDescription());
        taskEntity.setAddedBy(convertToUserDBEntity(taskEntityGWT.getAddedBy()));
        taskEntity.setAssignedTo(convertToUserDBEntity(taskEntityGWT.getAssignedTo()));
        taskEntity.setDueDate(convertStringToDate(taskEntityGWT.getDueDate()));
        taskEntity.setComplete(taskEntityGWT.isComplete());
        taskEntity.setCloseDescription(taskEntityGWT.getCloseDescription());
        return taskEntity;
    }


    public static UserEntity convertToUserDBEntity(UserEntityGWT userEntityGWT) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(userEntityGWT.getId());
        userEntity.setLogin(userEntityGWT.getLogin());
        return userEntity;
    }


    /*  public static List<TaskEntity> convertToTaskDBEntities(List<TaskEntityGWT> taskEntities, UserEntity addedBy) {
        List<TaskEntity> result = new ArrayList<TaskEntity>();
        for (TaskEntityGWT taskEntity : taskEntities) {
            TaskEntity taskEntityEx = convertToTaskDBEntity(taskEntity, addedBy);
            result.add(taskEntityEx);
        }
        return result;


    }


    public static TaskEntity convertToTaskDBEntity(TaskEntityGWT taskEntityGWT, UserEntity addedBy) {
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setId(taskEntityGWT.getId());
        taskEntity.setCreatedDate(convertStringToDate(taskEntityGWT.getCreatedDate()));
        taskEntity.setDescription(taskEntityGWT.getDescription());
        taskEntity.setAddedBy(addedBy != null ? addedBy :convertToUserDBEntity(taskEntityGWT.getAddedBy()));
        taskEntity.setAssignedTo(convertToUserDBEntity(taskEntityGWT.getAssignedTo()));
        taskEntity.setDueDate(convertStringToDate(taskEntityGWT.getDueDate()));
        taskEntity.setComplete(taskEntityGWT.isComplete());
        taskEntity.setCloseDescription(taskEntityGWT.getCloseDescription());
        return taskEntity;
    }


    public static UserEntity convertToUserDBEntity(UserEntityGWT userEntityGWT) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(userEntityGWT.getId());
        userEntity.setLogin(userEntityGWT.getLogin());
        userEntity.setAssignedEntities(convertToTaskDBEntities(userEntityGWT.getAssignedEntities(), userEntity));
        userEntity.setWritedEntites(convertToTaskDBEntities(userEntityGWT.getWritedEntites(), userEntity));
        return userEntity;
    }*/

    private static Date convertStringToDate(String date) {
        try {
            return FORMATTER.parse(date);
        } catch (ParseException e) {
            throw new IllegalStateException("Can't parse string to Date");
        }
    }


}
